package com.bop.dehong.huodong;

import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.config.Config;
import com.bop.dehong.info.GetHuodongInfo;
import com.bop.dehong.info.HuodongInfo;
import com.bop.dehong.webview.WebViewActivity;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

/**
* @author TangWenhan/YeFengyun
*2015年11月27日上午9:35:09
*/
public abstract class HuodongBaseFragment extends BaseFragment implements OnHeaderRefreshListener,OnFooterRefreshListener{
	
	public PullToRefreshView mRefreshView;
	public ListView mListView;
	public HuodongActivity mHuodongActivity;

	public final int REQ_REFRESH = 0;
	public final int REQ_LOAD  = 1;
	public int REQ_TYPE = 0;
	
	public int mCurrentPage = 0;
	public int mTotalPage = 0;
	HuodongListAdapter mAdapter;

	@Override
	protected void initTop() {
	}
	@Override
	protected void initUI() {
		mListView = getView(R.id.listView1);
		mRefreshView = getView(R.id.pullToRefreshView1);
		mRefreshView.setOnHeaderRefreshListener(this);
		mRefreshView.setOnFooterRefreshListener(this);
		mListView.setOnItemClickListener(this);
	}
	public abstract void reqData();
	
	

	@Override
	public void initData() {
		if (mAdapter == null) {
			mAdapter =  new HuodongListAdapter(this.getActivity());
		}
		mListView.setAdapter(mAdapter);
		if (mAdapter.size() <= 0) {
			reqData();
		}
	}
	public void initData(GetHuodongInfo info) {
		mRefreshView.onRefreshComplete();
		if (REQ_TYPE == REQ_REFRESH) {
			mAdapter.clear();
		}
		mTotalPage = info.totalpages;
		if (info.list.size() > 0) {
			mAdapter.addAll(info.list);
		}else {
			ToastUtils.show(getActivity(), "没有数据可加载");
		}
		mAdapter.notifyDataSetChanged();
	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mHuodongActivity = (HuodongActivity) activity;
	}
	
	

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		HuodongInfo info = (HuodongInfo) arg0.getAdapter().getItem(arg2);
		
		Intent intent = new Intent(getActivity(),WebViewActivity.class);
		intent.putExtra("title", info.title);
		intent.putExtra("url", info.linkurl);
		intent.putExtra(Config.GO_BACK, true);
		startActivity(intent);
	}
	@Override
	protected int getContentViewId() {
		return R.layout.frag_huodong_special;
	}
	
	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mRefreshView.onRefreshComplete();
		REQ_TYPE = REQ_LOAD;
		if (mCurrentPage  < mTotalPage) {
			mCurrentPage++;
			reqData();
		}else {
			ToastUtils.show(getActivity(), "没有数据可加载");
		}
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		mRefreshView.onRefreshComplete();
		REQ_TYPE = REQ_REFRESH;
		mCurrentPage = 1 ;
		reqData();
	}
}

